class ZCL_OTX_PF01_CL_MODULE_LINK definition
  public
  inheriting from /OTX/PF01_CL_MODULE_HANDLER
  create public .

*"* public components of class ZCL_OTX_PF01_CL_MODULE_LINK
*"* do not include other source files here!!!
public section.

  methods /OTX/PF01_IF_MODULE~PROCESS_MODULE
    redefinition .
protected section.
*"* protected components of class ZCL_OTX_PF01_CL_MODULE_LINK
*"* do not include other source files here!!!

  methods LINK_IMAGES
    exporting
      !PE_PROCESSED type XFELD
    exceptions
      ERROR_LINKAGE .
private section.
*"* private components of class ZCL_OTX_PF01_CL_MODULE_LINK
*"* do not include other source files here!!!
ENDCLASS.



CLASS ZCL_OTX_PF01_CL_MODULE_LINK IMPLEMENTATION.


METHOD /OTX/PF01_IF_MODULE~PROCESS_MODULE.

  DATA lv_processed  TYPE xfeld.

  IF mv_test IS INITIAL.
    CALL METHOD me->link_images
      IMPORTING
        pe_processed  = lv_processed
      EXCEPTIONS
        error_linkage = 1
        OTHERS        = 2.
    IF sy-subrc <> 0.
      RAISE error_processing.
    ENDIF.
  ENDIF.
* if we have not linked any image -> no log
  IF lv_processed IS INITIAL.
    pe_no_log = abap_true.
  ENDIF.

*--------------------------------------------------------------------*
* Regular Processing
  CALL METHOD super->/otx/pf01_if_module~process_module
    IMPORTING
      pe_status        = pe_status
      pe_in_process    = pe_in_process
      pe_timeout       = pe_timeout
      pes_1reg         = pes_1reg
    EXCEPTIONS
      error_processing = 0
      OTHERS           = 0.
*--------------------------------------------------------------------*
ENDMETHOD.


METHOD LINK_IMAGES.

  DATA LV_OBJECT_ID TYPE SAEOBJID.
  DATA LT_CONN      TYPE TABLE OF TOAV0.
  DATA LT_CONN_REG  TYPE TABLE OF TOAV0.
  DATA LS_CONN      TYPE TOAV0.
  DATA LS_CONN1      TYPE TOAV0.
  DATA LH_CUST      TYPE REF TO /OTX/PF01_CL_CUSTOMIZING.
  DATA LV_CUSVAL    TYPE /OTX/PF01_E_CUSVAL.
  DATA LV_MSGV1     TYPE MSGV1.
  DATA LV_MSGV2     TYPE MSGV2.
  DATA LT_1IMG      TYPE TABLE OF /OTX/PF01_T_1IMG.
  DATA LS_1IMG      TYPE /OTX/PF01_T_1IMG.
  DATA LV_DOC_TYPE  TYPE SAEDOKTYP.
  DATA LV_LINKED    TYPE XFELD.
  DATA LV_FILENAME  TYPE CHAR255.                           "BOB-4005
  DATA LS_RETURN    TYPE BAPIRET2.
  DATA LH_AL_ACCESS TYPE REF TO /OTX/PF00_IF_AL_ACCESS.     "BOB-9200

  DATA:
    LV_OBJECTTYPE TYPE TOAV0-SAP_OBJECT,
    LS_TOAAT      TYPE TOAAT,
    LT_TOAAT      TYPE STANDARD TABLE OF TOAAT.

  "获取发票结算单附件，并链接到DP对象
  SELECT SINGLE
    JSDDH
    INTO LV_OBJECT_ID
    FROM ZFIT079_HEAD
   WHERE EDI_DOCNUM = MS_1REG-EDI_DOCNUM.
  IF SY-SUBRC = 0.

    LV_OBJECTTYPE = 'ZZFI_FPJSD'.
    "Get all existing connections
    CALL FUNCTION 'ARCHIV_GET_CONNECTIONS'
      EXPORTING
        OBJECTTYPE      = LV_OBJECTTYPE
        OBJECT_ID       = LV_OBJECT_ID
      TABLES
        CONNECTIONS     = LT_CONN
        FILE_ATTRIBUTES = LT_TOAAT
      EXCEPTIONS
        NOTHING_FOUND   = 0
        OTHERS          = 0.

    IF LT_CONN[] IS NOT INITIAL.

      "取已经注册过的
      LV_OBJECT_ID = MS_1REG-REGID.
      CALL FUNCTION 'ARCHIV_GET_CONNECTIONS'
        EXPORTING
          OBJECTTYPE    = /OTX/PF01_IF_CONSTANTS_IDF=>MC_OBJECT
          OBJECT_ID     = LV_OBJECT_ID
        TABLES
          CONNECTIONS   = LT_CONN_REG
        EXCEPTIONS
          NOTHING_FOUND = 0                                     "BOB-7885
          OTHERS        = 0.

      SORT LT_CONN_REG BY ARCHIV_ID ARC_DOC_ID.
      SORT LT_TOAAT BY ARC_DOC_ID.
      LOOP AT LT_CONN INTO LS_CONN.

        "关联过的不再关联
        READ TABLE LT_CONN_REG TRANSPORTING NO FIELDS WITH KEY
          ARCHIV_ID = LS_CONN-ARCHIV_ID
          ARC_DOC_ID = LS_CONN-ARC_DOC_ID
          BINARY SEARCH.
        IF SY-SUBRC = 0.
          CONTINUE.
        ENDIF.

        CLEAR LV_DOC_TYPE.
        LH_AL_ACCESS = /OTX/PF00_CL_AL_ACCESS=>GET_INSTANCE( ).
        TRY.
            LV_DOC_TYPE = LH_AL_ACCESS->GET_DOC_TYPE( IV_AR_OBJECT = LS_CONN-AR_OBJECT ).
          CATCH /OTX/CX_PF00_EXCEPTION.                 "#EC NO_HANDLER
        ENDTRY.
*        IF LV_DOC_TYPE IS INITIAL
*          OR LV_DOC_TYPE = '*'.                             "BOB-1200
*          LV_DOC_TYPE = LS_CONN-DOC_TYPE.
*        ENDIF.

        CLEAR LS_TOAAT.
        READ TABLE LT_TOAAT INTO LS_TOAAT
          WITH KEY ARC_DOC_ID = LS_CONN-ARC_DOC_ID BINARY SEARCH.
        LV_FILENAME = LS_TOAAT-FILENAME.

        CLEAR LS_RETURN.
        CALL METHOD /OTX/PF00_CL_UTILITIES=>ARCHIVE_CONNECTION_INSERT "BOB-4005
          EXPORTING
            PI_ARC_DOC_ID = LS_CONN-ARC_DOC_ID
            PI_AR_DATE    = SY-DATUM
            PI_AR_OBJECT  = LS_CONN-AR_OBJECT "归档文件类型，比如ZINV_PDF
            PI_OBJECT_ID  = LV_OBJECT_ID      "业务单号
            PI_SAP_OBJECT = /OTX/PF01_IF_CONSTANTS_IDF=>MC_OBJECT   "业务单据类型
            PI_DOC_TYPE   = LV_DOC_TYPE       "附件类型，比如PDF、XML等
            PI_FILENAME   = LV_FILENAME
            PI_CREATOR    = SY-UNAME
          IMPORTING
            PES_RETURN    = LS_RETURN.
        IF LS_RETURN IS NOT INITIAL.
          "add AppLog
          LV_MSGV1 = LS_CONN-AR_OBJECT.
          LV_MSGV2 = 'CONNECTION_INSERT'.
          CALL METHOD MH_LOG->ADD_MESSAGE
            EXPORTING
              PI_LEVEL = /OTX/PF01_IF_CONSTANTS_LOG=>MC_LEVEL_2
              PI_MSGID = /OTX/PF01_IF_CONSTANTS_LOG=>MC_MSGID
              PI_MSGNO = /OTX/PF01_IF_CONSTANTS_LOG=>MC_MSGNO_044
              PI_MSGV1 = LV_MSGV1
              PI_MSGV2 = LV_MSGV2.
          RAISE ERROR_LINKAGE.
        ELSE.
          PE_PROCESSED  = ABAP_TRUE.
        ENDIF.

        " set leading image 必须要有，不然DP中附件都不存在了
        IF LS_CONN-AR_OBJECT = 'ZINV_PDF'.
          IF MS_1REG-ARC_DOC_ID IS INITIAL.
            MS_1REG-ARC_DOC_ID = LS_CONN-ARC_DOC_ID.
            MS_1REG-ARCHIV_ID  = LS_CONN-ARCHIV_ID.
            MS_1REG-AR_OBJECT  = LS_CONN-AR_OBJECT.
          ENDIF.
        ENDIF.

        LS_CONN1 = LS_CONN.
        AT LAST.
          IF MS_1REG-ARC_DOC_ID IS INITIAL.
            MS_1REG-ARC_DOC_ID = LS_CONN1-ARC_DOC_ID.
            MS_1REG-ARCHIV_ID  = LS_CONN1-ARCHIV_ID.
            MS_1REG-AR_OBJECT  = LS_CONN1-AR_OBJECT.
          ENDIF.
        ENDAT.
      ENDLOOP.
    ENDIF.
  ENDIF.
ENDMETHOD.
ENDCLASS.
